<script setup lang="ts">
import {useRouter} from 'vue-router';
import {ref} from "vue";
import request from '../plugins/request.js'
import {showFailToast, showSuccessToast} from "vant";

const router = useRouter()

const userAccount = ref('');
const userPassword = ref('');
const onSubmit = async () => {
  const result = await request.post('/user/login', {
    userAccount: userAccount.value,
    userPassword: userPassword.value
  })
  const {code, data} = result;
  if (code ===200 && data){
    showSuccessToast('登录成功')
    await router.replace('/')
  }else {
     showFailToast('登录失败')
  }

};

</script>
<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="userAccount"
          label="用户名"
          placeholder="请输入用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="userPassword"
          type="userPassword"
          name="密码"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<style scoped></style>